package com.itlin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itlin.bean.Dish;
import com.itlin.bean.PageParam;
import com.itlin.bean.Setmeal;
import com.itlin.common.R;
import com.itlin.dto.SetmealDto;
import com.itlin.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 所谓伊人
 * @date 2022/3/26 - 21:11
 */
@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto) {

        int row = setmealService.add(setmealDto);

        if (row > 0) {
            return R.success("添加套餐成功");
        }

        return R.error("添加套餐失败");
    }

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){

        IPage<SetmealDto> page = setmealService.findPage(pageParam);
        return R.success(page);

    }

    /**
     * 删除套餐 【批量删除】
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        System.out.println("ids==" + ids);
        int row = setmealService.delete(ids);
        if(row > 0){
            return R.success("删除套餐成功！");
        }else{
            return R.error ("删除套餐失败！");
        }
    }

    @GetMapping("/list")
    public R<List<Setmeal>> findByCategoryId(long categoryId, Integer status) {

        List<Setmeal> dishList = setmealService.findByCategoryId(categoryId, status);

        return R.success(dishList);
    }

//    批量起售和停售
    @PostMapping("/status/{status}")
    public R updateStatus(Setmeal setmeal,@RequestParam List<Long> ids){
        System.out.println(ids);
        int row = setmealService.updateStatus(setmeal, ids);
        if (row>0){
            return R.success("修改成功");
        }
        else {
            return R.error("修改失败");
        }

    }

//    修改数据回显
    @GetMapping("/{id}")
    public R show(@PathVariable Long id){
        System.out.println(id);
        SetmealDto setmealDto = setmealService.show(id);
        return R.success(setmealDto);
    }

//    修改数据
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
        int i = setmealService.updateSeatmeal(setmealDto);
        if (i>0){
            return R.success("修改成功");
        }
        else return R.error("修改失败");
    }
}
